package com.sojson.annotation.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component;

import com.sojson.config.condition.TestCondition;
import com.sojson.constant.ConstantServlet;
import com.sojson.util.log.LogUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 测试环境下的切面
 * 
 * @author liu
 * @date 2021-05-18
 */
@Slf4j
@Aspect
@Component
/** 是否注入这个Bean */
@Conditional(TestCondition.class)
public class TestAspect extends PointcutsWebBase {

    /** 配置织入点 */
    @Pointcut("@annotation(" + ConstantServlet.PACKAGE_DEFAULT_ANNOTATION_ALL + ".CustomTest)")
    public void customTest() {}

    /** 切点 */
    private static final String POINTCUT = POINTCUT_CONTROLLER + "||customTest()";

    /**
     * 请求处理之前和之后
     * 
     * @param pjp 切点
     * @return 接口返回的内容,可以拦截(这里如果不返回就等于接口不返回)
     * @throws Throwable
     */
    @Around(POINTCUT)
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        // 输出公共数据
        LogUtil.outConsole(log);

        long currentTimeMillis = System.currentTimeMillis();
        // 获取接口返回值
        Object proceed = pjp.proceed();
        log.info("接口执行完毕,执行时间: " + (System.currentTimeMillis() - currentTimeMillis) + " 毫秒");

        return proceed;
    }

}